﻿using Element.Net.Controls;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace Element.Net.Dialogs
{
    public enum MessageIcons
    {
        Error,
        Info,
        Warn,
        Success
    }

    public partial class ConfirmDialog : Form
    {
        private MessageIcons messageIcon;

        [Browsable(true)]
        public MessageIcons MessageIcon
        {
            get => messageIcon;
            set
            {
                messageIcon = value;
                switch (messageIcon)
                {
                    case MessageIcons.Success:
                        pictureBox1.Image = Properties.Resources.success;
                        break;
                    case MessageIcons.Info:
                        pictureBox1.Image = Properties.Resources.info;
                        break;
                    case MessageIcons.Warn:
                        pictureBox1.Image = Properties.Resources.warn;
                        break;
                    case MessageIcons.Error:
                        pictureBox1.Image = Properties.Resources.error;
                        break;
                }
            }
        }

        public ConfirmDialog(string message = "", MessageIcons icon = MessageIcons.Info)
        {
            InitializeComponent();
            InitConfirmDialog();
            label1.Text = message;
            MessageIcon = icon;
            zone2.Text = "确定";
            zone3.Text = "取消";
        }

        private void InitConfirmDialog()
        {
            this.SetStyle(
             ControlStyles.UserPaint |  //控件自行绘制，而不使用操作系统的绘制
             ControlStyles.AllPaintingInWmPaint | //忽略擦出的消息，减少闪烁。
             ControlStyles.OptimizedDoubleBuffer |//在缓冲区上绘制，不直接绘制到屏幕上，减少闪烁。
             ControlStyles.ResizeRedraw | //控件大小发生变化时，重绘。                  
             ControlStyles.SupportsTransparentBackColor, true);//支持透明背景颜色
            base.UpdateStyles();
        }

        /// <summary>
        /// 绘制圆角路径
        /// </summary>
        private GraphicsPath GetGraphicsPath(Rectangle rect, float radius)
        {
            GraphicsPath graphicsPath = new GraphicsPath();
            graphicsPath.AddArc(rect.X, rect.Y, radius, radius, 180, 90);
            graphicsPath.AddArc(rect.Width - radius, rect.Y, radius, radius, 270, 90);
            graphicsPath.AddArc(rect.Width - radius, rect.Height - radius, radius, radius, 0, 90);
            graphicsPath.AddArc(rect.X, rect.Height - radius, radius, radius, 90, 90);
            graphicsPath.CloseAllFigures();
            return graphicsPath;
        }

        /// <summary>
        /// 高质量绘制矩形
        /// </summary>
        /// <param name="graphics"></param>
        /// <param name="rectangle"></param>
        /// <param name="color"></param>
        private void DrawRectangle(Graphics graphics, Rectangle rectangle, Color color, float radius)
        {
            graphics.SmoothingMode = SmoothingMode.HighQuality;
            graphics.CompositingQuality = CompositingQuality.HighQuality;
            graphics.InterpolationMode = InterpolationMode.HighQualityBilinear;
            GraphicsPath graphicsPath = GetGraphicsPath(rectangle, radius);
            SolidBrush solidBrush = new SolidBrush(color);
            graphics.FillPath(solidBrush, graphicsPath);
        }

        /// <summary>
        /// 重绘按钮
        /// </summary>
        /// <param name="pevent"></param>
        protected override void OnPaint(PaintEventArgs pevent)
        {
            base.OnPaint(pevent);
            Graphics graphics = pevent.Graphics;
            Rectangle rectangle = new Rectangle(0, 0, Width, Height);
            DrawRectangle(graphics, rectangle, BackColor, 30);
        }

        protected override void OnPaintBackground(PaintEventArgs pevent)
        {
            //base.OnPaintBackground(pevent);
            Graphics graphics = pevent.Graphics;
            Rectangle rectangle = new Rectangle(0, 0, Width, Height);
            DrawRectangle(graphics, rectangle, Color.Transparent, 1f);
        }

        private void OnClicked(object sender, EventArgs e)
        {
            if (sender is ElementZone zone)
            {
                switch (zone.Name)
                {
                    case "zone2":
                        DialogResult = DialogResult.OK;
                        break;
                    case "zone3":
                        DialogResult = DialogResult.Cancel;
                        break;
                }
                Close();
                Dispose();
            }
        }
    }
}
